<?php 
namespace Home\Controller;
use \Think\Controller;
/**
*用户登陆数据处理
 */
class LoginController extends Controller
{

    public function index() 
    {
        $this->assign('title','[摆渡登陆]--[注册即送50金币50积分~]');
        $this->assign('header','[欢迎登陆]');
        
        $this->display();
    }


    /**
    * 验证码
     */
    // public function vcode()
    // {
    //     $config = array(
    //         'fontSize' => 30, // 验证码字体大小
    //         'length' => 5, // 验证码位数
    //         'useNoise' => false, // 关闭验证码杂点
    //         'bg' => array(mt_rand(188,255), mt_rand(188,255), mt_rand(188,255)),
    //     );
    //     $Verify = new \Think\Verify($config);
    //     $Verify->entry();
    // }
    
    
    // 生成验证码
    public function vcode()
    {
        $config = array(
            'fontSize' => 40, // 验证码字体大小
            'length' => 4, // 验证码位数
            'imageW'=>300,
            'useNoise' => true, // 关闭验证码杂点
            'useCurve'=>false,//关闭曲线
            'useImgBg'=>true,//使用背景图片
            // 'bg' => array(mt_rand(222,255), mt_rand(222,255), mt_rand(222,255)),
        );
        $Verify = new \Think\Verify($config);
        $Verify->codeSet = '0123456789';
        $Verify->entry();
    }


    // 验证登陆
    public function verify()
    {
        if (IS_POST) {
            // 验证码验证
            $vcode = I('post.vcode');
            $verify = new \Think\Verify();
            $id = '';
            if (!$verify->check($vcode)) {
                $this->error('验证码有误!');
                exit;
            }
            // 接收用户提交信息
            $uname = I('post.username');
            $pwd = md5(I('post.password'));
            // 根据用户提交的用户名从数据库中查找
            $user = M('user')->where("name = '$uname'")->find();
            // 判断用户和密码
            if (!$user || $user['password'] != $pwd) $this->error('用户名或者密码错误');
            $uid = $user['id'];
            $user_info = M('user_info')->where("id ='$uid'")->find();
            if ($user_info['status'] == 2) $this->error('账号状态异常!');

            // 一切通过登陆成功,写入session并跳转
            $id = $user['id'];
            $user_info = M('user_info')->where('uid = '.$id)->find();
            // exit;
            $_SESSION['user'] = $user_info;
            $_SESSION['user']['name'] = $user['name'];
            $_SESSION['user']['email'] = $user['email'];
            $_SESSION['user']['phone'] = $user['phone'];
            // 判断该用户今天是否登录
            $register = D('register');
            $where['date'] = date('Ymd');
            $where['uid'] = $uid;
            // 我的签到
            $register_num = $register->where('uid ='.$_SESSION['user']['uid'])->count();
            if (!$register_num) {
                $register_num = 0;
            }
            $_SESSION['user']['register_num'] = $register_num;


            if ($register->where($where)->find() != null) {
                $_SESSION['user']['register'] = 'registered';
            } else{
                $_SESSION['user']['register'] = '';
            }
            $this->success('登陆成功',U('Index/index'),1);
        } else{
            $this->error('非法操作!','',1);
        }
        

    }
}